﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Prism.Commands;
using Prism.Events;
using System.Windows.Input;
using Prism.Mvvm;
using SoonCode.Core;
using SoonCode.FocusTest.Events;
using SoonCode.FocusTest.Models;
using SoonCode.FocusTest.Utils;

namespace SoonCode.FocusTest.ViewModels
{
    class TestItemBViewModel:BindableBase
    {
        private int TestItemIndex = 2;
        private RandomHelper randomHelper = new RandomHelper(1, 10);
        private int tabIndex = 0;
        private int bits = 2;
        private int err = 0;
        private TestInfo testInfo;
        private IEventAggregator aggregator;
        private string helpSoundText = "停止播放提示语";
        private bool helpIsPlay = true;
        public string HelpSoundText
        {
            get { return helpSoundText; }
            set
            {
                helpSoundText = value;
                RaisePropertyChanged();
            }
        }
        public int TabIndex
        {
            get { return tabIndex; }
            set
            {
                tabIndex = value;
                RaisePropertyChanged();
            }
        }

        private string numbers;

        public string Numbers
        {
            get { return numbers; }
            set
            {
                numbers = value;
                RaisePropertyChanged();
            }
        }

        public TestItemBViewModel(IEventAggregator aggregator)
        {
            this.aggregator = aggregator;
            this.aggregator.GetEvent<TestLoadEvent>().Subscribe(OnLoaded);
        }

        public void OnLoaded(int p)
        {
            if (p != this.TestItemIndex - 1) return;
            this.testInfo = GlobalModel.Instance.TestInfo;
            SoundPlayer.Play("1000:item2.mp3");
            TabIndex = 0;
            bits = 2;
            err = 0;
        }
        public ICommand HelpCommand
        {
            get
            {
                return new DelegateCommand(() =>
                {
                    if (helpIsPlay)
                    {
                        SoundPlayer.Stop();
                        HelpSoundText = "播放提示语";
                        helpIsPlay = false;
                    }
                    else
                    {
                        SoundPlayer.Play("item2.mp3");
                        HelpSoundText = "停止播放提示语";
                        helpIsPlay = true;
                    }
                });
            }
        }
        public ICommand SkipCommand
        {
            get
            {
                return new DelegateCommand(() =>
                {
                    SoundPlayer.Stop();
                    this.aggregator.GetEvent<ChangeTestEvent>().Publish(TestItemIndex);
                });
            }
        }
        public ICommand StartCommand
        {
            get
            {
                return new DelegateCommand(() =>
                {
                    TabIndex = 1;
                    Test();
                });
            }
        }

        public ICommand AnswerErrorCommand
        {
            get
            {
                return new DelegateCommand(() =>
                {
                    this.err++;
                    if (err >= 2)
                    {
                        SoundPlayer.Stop();
                        complate();
                        return;
                    }
                    Test();
                });
            }
        }
        public ICommand AnswerRightCommand
        {
            get
            {
                return new DelegateCommand(() =>
                {
                    this.err = 0;
                    this.bits++;
                    if (bits > 20)
                    {
                        complate();
                        return;
                    }
                    Test();
                });
            }
        }

        private void complate()
        {
            DialogUtil.Show(aggregator, $"位数能力=（{bits - 1}）位，本项结束，进入下一项");
            testInfo.data[TestItemIndex-1].item_score = (int)((double)(bits-1) / 20 * 100) + "";
            this.aggregator.GetEvent<ChangeTestEvent>().Publish(TestItemIndex);
        }
        private void Test()
        {
            string numbers = randomHelper.next(bits);
            string t = "";
            foreach (var s in numbers)
            {
                if (!t.IsNullOrEmpty())
                    t += "-";
                t += s;
            }

            this.Numbers = t;
            SoundPlayer.PlayNumberString(numbers, () =>
            {
                Thread.Sleep(600); 
                SoundPlayer.PlayDing();
            });
        }
    }
}
